import { createRouter, createWebHistory } from 'vue-router'
import MainLayout from '../layout/MainLayout.vue'

const routes = [
    {
        path: '/',
        component: MainLayout,
        redirect: '/dashboard',
        meta: { requiresAuth: true },
        children: [
            {
                path: 'dashboard',
                name: 'Dashboard',
                component: () => import('../views/Dashboard.vue'),
                meta: { requiresAuth: true }
            },
            {
                path: 'suppliers',
                name: 'SupplierManagement',
                component: () => import('../views/SupplierManagement.vue'),
                meta: { requiresAuth: true }
            },
            {
                path: 'purchasers',
                name: 'PurchaserManagement',
                component: () => import('../views/PurchaserManagement.vue'),
                meta: { requiresAuth: true }
            },
            {
                path: 'users',
                name: 'UserManagement',
                component: () => import('../views/UserManagement.vue'),
                meta: { requiresAuth: true }
            },
            {
                path: 'roles',
                name: 'RoleManagement',
                component: () => import('../views/RoleManagement.vue'),
                meta: { requiresAuth: true }
            },
            {
                path: 'logs',
                name: 'LogMonitor',
                component: () => import('../views/LogMonitor.vue'),
                meta: { requiresAuth: true }
            },
            {
                path: 'menu-setting',
                name: 'MenuSetting',
                component: () => import('../views/system/MenuSetting.vue'),
                meta: { requiresAuth: true }
            },
            {
                path: 'form-builder',
                name: 'FormBuilder',
                component: () => import('../views/system/FormBuilder.vue'),
                meta: { requiresAuth: true }
            },
        ]
    },
    {
        path: '/login',
        name: 'Login',
        component: () => import('../views/Login.vue'),
        meta: { requiresAuth: false }
    }
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

// 路由导航守卫
router.beforeEach((to, from, next) => {
    const token = localStorage.getItem('token')
    const requiresAuth = to.matched.some(record => record.meta.requiresAuth)

    // 如果需要登录认证
    if (requiresAuth) {
        // 检查是否有token
        if (!token) {
            // 没有token，重定向到登录页
            next({
                path: '/login',
                query: { redirect: to.fullPath }
            })
        } else {
            // 有token，允许访问
            next()
        }
    } else {
        // 不需要登录认证的页面（如登录页）
        if (to.path === '/login' && token) {
            // 如果已登录且访问登录页，重定向到首页
            next({ path: '/dashboard' })
        } else {
            // 允许访问
            next()
        }
    }
})

export default router 